package fr.univavignon.m1informatique.rgla.is.ui;

import javax.swing.JOptionPane;

import fr.univavignon.m1informatique.rgla.is.LoginManager;
import fr.univavignon.m1informatique.rgla.is.PKITest;

/**
 * 
 * @author DRAMSY Christian
 */
public class LoginFrame extends javax.swing.JFrame {

	private PKITest father;

	/** Creates new form LoginFrame */
	public LoginFrame(PKITest father) {
		initComponents();
		this.father = father;
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

		authentificationLabel = new javax.swing.JLabel();
		loginLabel = new javax.swing.JLabel();
		passwordLabel = new javax.swing.JLabel();
		loginTextField = new javax.swing.JTextField();
		passwordTextField = new javax.swing.JTextField();
		loginButton = new javax.swing.JButton();

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

		authentificationLabel.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
		authentificationLabel.setText("Authentification");

		loginLabel.setText("Login ");

		passwordLabel.setText("Password");

		loginTextField.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				tryLog(evt);
			}
		});

		passwordTextField
				.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent evt) {
						tryLog(evt);
					}
				});

		loginButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				tryLog(evt);
			}
		});

		loginButton.setText("Login");

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.LEADING)
												.addGroup(
														layout.createSequentialGroup()
																.addGap(61, 61,
																		61)
																.addComponent(
																		authentificationLabel))
												.addGroup(
														layout.createSequentialGroup()
																.addContainerGap()
																.addGroup(
																		layout.createParallelGroup(
																				javax.swing.GroupLayout.Alignment.LEADING)
																				.addComponent(
																						loginLabel)
																				.addComponent(
																						passwordLabel))
																.addGap(27, 27,
																		27)
																.addGroup(
																		layout.createParallelGroup(
																				javax.swing.GroupLayout.Alignment.LEADING,
																				false)
																				.addComponent(
																						passwordTextField)
																				.addComponent(
																						loginTextField,
																						javax.swing.GroupLayout.DEFAULT_SIZE,
																						142,
																						Short.MAX_VALUE)
																				.addComponent(
																						loginButton,
																						javax.swing.GroupLayout.Alignment.TRAILING))))
								.addContainerGap(
										javax.swing.GroupLayout.DEFAULT_SIZE,
										Short.MAX_VALUE)));
		layout.setVerticalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addComponent(authentificationLabel)
								.addGap(34, 34, 34)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(loginLabel)
												.addComponent(
														loginTextField,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE))
								.addGap(18, 18, 18)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(passwordLabel)
												.addComponent(
														passwordTextField,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE))
								.addGap(39, 39, 39)
								.addComponent(loginButton)
								.addContainerGap(
										javax.swing.GroupLayout.DEFAULT_SIZE,
										Short.MAX_VALUE)));

		pack();
		this.setLocationRelativeTo(null);
	}// </editor-fold>

	private void tryLog(java.awt.event.ActionEvent evt) {
		// TODO add your handling code here:
		if (!this.loginTextField.getText().isEmpty()
				&& !this.passwordTextField.getText().isEmpty()) {
			if (LoginManager.tryConnect(this.loginTextField.getText(),
					this.passwordTextField.getText())) {

				this.father.launchPKI(this.loginTextField.getText());
				this.hide();
			} else {
				(new JOptionPane()).showMessageDialog(null,
						"Login/Mot de passe incorrect!", "Information",
						JOptionPane.INFORMATION_MESSAGE);
			}
		} else {
			(new JOptionPane()).showMessageDialog(null,
					"Remplissez tout les champs", "Information",
					JOptionPane.INFORMATION_MESSAGE);
		}
	}

	// Variables declaration - do not modify
	private javax.swing.JLabel authentificationLabel;
	private javax.swing.JButton loginButton;
	private javax.swing.JLabel loginLabel;
	private javax.swing.JTextField loginTextField;
	private javax.swing.JLabel passwordLabel;
	private javax.swing.JTextField passwordTextField;
	// End of variables declaration
}
